<?php
//$Id: sms.php 9 2009-04-26 06:35:12Z bole.Chen $
//短信提醒
uses('model');

class Sms extends Object {
    //短信王 - 增值服务，需要用户名和密码
    var $baseurl = 'http://www.china-sms.com/send/gsend.asp?name=&pwd=&dst=%s&msg=%s';
    var $msg;

    //检查是否正确的手机号
    function ckmobile($mobile) {
        return preg_match('/^1[35][0-9]{9}$/', trim($mobile));
    }

    //新订单通知
    function newIndent($iid) {
        $this->setMsg('新订单:'.$iid);
        $this->sendMsgToDb($this->cfg['admin_mobile']);
    }

    //付款成功通知
    function payIndent($mobile, $iid, $fee=0) {
        $this->debugmsg('Sms::payIndent');

        $this->setMsg("我们已经收到了您的{$iid}号订单付款，稍候将安排给您发货。");
        $this->sendMsgToDb($mobile);
        $this->setMsg("{$iid}号订单已付款{$fee}元。");
        $this->sendMsgToDb($this->cfg['admin_mobile']);
    }

    //订单取消
    function cancelIndent($mobile, $iid) {
        $this->setMsg("因至今未收到付款，我们取消了您的{$iid}号订单，如有所需请到乐铺上再订购。");
        $this->sendMsgToDb($mobile);
    }

    //订单发货
    function postIndent($mobile, $iid, $postid, $postname) {
        $this->setMsg("您的订单已通过{$postname}发出，单号{$postid}请注意查收！");
        $this->sendMsgToDb($mobile);
    }

    //插入短信队列
    function sendMsgToDb($mobile, $msg="", $postfix=true) {
        $this->debugmsg('Sms::sendMsgToDb');
        if ($msg != "") $this->msg = $msg;
        //if ($postfix) $this->msg .= '有疑问请询4006767167。乐铺';
        if ($postfix) $this->msg .= '有疑问请询059187272707乐铺';
        $msg = $this->msg;

        if ($msg && $this->_msglen($msg) <= 70 && $this->ckmobile($mobile)) {

            $m =& new Model;
            $m->dbconnect();

            $fields['mobile']   = $mobile;
            $fields['msg']      = $msg;
            $fields['created']  = time();

            $m->db->Insert($m->table['sms'], $fields);

        } else {
            return false;
        }
    }

    //下行短信
    function sendMsg($mobile, $msg="") {
        //测试时不发送
        if (defined('ISLOCAL')) return true;

        if ($msg != "") $this->msg = $msg;
        if ($this->_msglen($this->msg) > 70) $err = 'Msg to long';
        $msg = $this->_utf82gb2312($this->msg);

        if ($msg && !$err && $this->ckmobile($mobile)) {
            $this->debugmsg(sprintf($this->baseurl, $mobile, $msg));
            $result_str = @file_get_contents(sprintf($this->baseurl, $mobile, $msg));
            return true; //总是发送成功
            parse_str($result_str, $result);
            if ($result['num'] > 0) {
                //$this->logger('[Succeed] '.$mobile.' '.$msg, 'sms.log');
                return true;
            } else {
                $this->debugmsg($result['err']);
                //$this->logger('[Fail:'. $result['err'] .'] '.$mobile.' '.$msg, 'sms.log');
                return $result['err'];
            }
        } else {
            $this->debugmsg($err);
            return $err ? $err : false;
        }
    }

    function setMsg($msg) {
        $this->msg = $msg;
    }

    function _msglen($str) {
        $str = $this->_utf82gb2312($str);
        return strlen(preg_replace("/[".chr(0xa1)."-".chr(0xff)."]{2}/", " ", $str));
    }

    function _utf82gb2312($str) {
        return iconv('UTF-8', 'GB2312', $str);
    }
}
?>
